//
//  ViewController.m
//  DrawingBoard
//
//  Created by ZJ on 2018/8/10.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ViewController.h"
#import "ZJView.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet ZJView *zjView;
@property (weak, nonatomic) IBOutlet UISlider *lineWidth;
@property (weak, nonatomic) IBOutlet UIButton *firstButton;

@end

@implementation ViewController

// 清屏
- (IBAction)clear:(id)sender {
    [self.zjView clear];
}

// 回退
- (IBAction)back:(id)sender {
    [self.zjView back];
}

// 橡皮
- (IBAction)eraser:(id)sender {
    [self.zjView eraser];
}

// 保存到相册
- (IBAction)save:(id)sender {
    // 1.开启图片类型的上下文
    UIGraphicsBeginImageContextWithOptions(self.zjView.bounds.size, NO, 0);
    
    // 4.获取当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 3.截图
    [self.zjView.layer renderInContext:ctx];
    
    // 5.取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 2.关闭上下文
    UIGraphicsEndImageContext();
    
    // 6.保存到相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

// 监听颜色的改变
- (IBAction)lineColorChange:(UIButton *)sender {
    // 设置颜色
    self.zjView.lineColor = sender.backgroundColor;
}

// 监听线宽改变
- (IBAction)lineWidthChange:(UISlider *)sender {
    // 把最新的数值(线宽)给了 zjView
    self.zjView.lineWidth = sender.value;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 默认线宽
    self.zjView.lineWidth = self.lineWidth.value;
    
    // 点一下第一个按钮
    [self lineColorChange:self.firstButton];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
